package com.loe.test

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.loe.ktui.*
import com.loe.ktui.custom.*
import com.loe.ktui.util.KtColor
import com.loe.ktui.util.KtFragItem
import com.loe.ktui.util.KtShape
import com.loe.ktui.util.ui_Shape

class MainActivity : AppCompatActivity()
{
    private val textDemo by lazy { findView<KtTextView>("textDemo") }

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)

        contentUI = ui_DrawerLayout()
        {
            width = MATCH_PARENT
            height = MATCH_PARENT
            bg = KtColor.yellow

            ui_RelativeLayout()
            {
                bg = KtColor.bg

                ui_TitleView("titleView")
                {
                    title = "测试标题"
                    bg = KtColor.white
                    statusFull = true
                }

                ui_View()
                {
                    below = "titleView"
                    transZ = 1
                    height = 6
                    bg = KtShape.shadowTop
                }

                ui_List<String>()
                {
                    bg = KtColor.white
                    height = 100
                    width = 200
                    transZ = 1
                    below = "titleView"
                    orientation = HORIZONTAL
                    scrollbars = HORIZONTAL
                    headerUI = ui_LinearLayout()
                    {
                        ui_Text()
                        {
                            padding = 20
                            text = "几号回家待发货富东饭店"
                            isAlphaClick = true
                            onClick()
                            {
                                this as KtTextView
                                text = "洪水过后大使馆的故事是打广告的的感受到个个都是多个多个山东干很多事搞活动时更好地更合适的规划"
                            }
                        }
                    }
                    itemUI = listItem

                    itemConvert()
                    { adapter, holder, bean ->
                        val title = findView<TextView>("textTitle")
                        title.text = bean
                    }

                    onLoadData()
                    {
                        postDelayed({
                            loadOk(listOf("新加的", "好的", "世界电话辅导费", "手机号的", "大家都放假京东方"), it)
                        }, 500)
                    }

                    datas = listOf("223是的是的", "大多都是", "刷卡说的就是", "第三方付", "疯疯癫癫", "大幅度", "方法", "是多少顺丰到付", "大多都是", "刷卡说的就是", "第三方付", "疯疯癫癫", "大幅度", "方法", "是多少顺丰到付")

                    onCreateView()
                    {
//                    setData(arrayListOf("3332稍等", "好的"))
                    }
                }

                ui_ScrollView()
                {
                    below = "titleView"
                    height = MATCH_PARENT
                    isRefresh = true

                    ui_LinearLayout()
                    {
                        width = MATCH_PARENT
                        padding = 8
                        divider = ui_Shape { size = 10 }

                        ui_FormEditView()
                        {
                            title = "地方的法规"
                            text = "几号发货返回"
                            textSize = 15
                            bg = KtColor.yellow
                            corner = 5
                        }

                        ui_HorizontalScrollView()
                        {
                            scrollBarSize = 0
                            onScroll()
                            {
                                Log.d("runtime", "scroll: $it")
                            }

                            ui_LinearLayout()
                            {
                                divider = ui_Shape { size = 10 }
                                orientation = HORIZONTAL
                                ui_Text()
                                {
                                    height = 50
                                    bg = KtColor.orange
                                    text = "就红三代集合上多喝点水节点节点节点节"
                                }
                                ui_Text()
                                {
                                    height = 50
                                    bg = KtColor.orange
                                    text = "jsdfj经济开发富家大室老地方砍价多少将地上放4838点精神亢奋几点开始绝对是客服扶汉室的框架返回的是尽快发货的数据库放的地"
                                }
                            }
                        }

                        ui_BannerView()
                        {
                            corner = 8
                            height = 180

                            itemUI = ui_FrameLayout()
                            {
                                ui_Image()
                                {
                                    id = "image"
                                    width = MATCH_PARENT
                                    height = MATCH_PARENT
                                    scaleType = ImageView.ScaleType.CENTER_CROP
                                }
                            }

                            itemConvert()
                            { bean, i ->
                                val image = findView<ImageView>("image")
                                image.src = bean.imageUrl
                            }

                            datas = listOf(
                                BannerBean("https://img0.baidu.com/it/u=705102279,1721672537&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"),
                                BannerBean("https://img2.baidu.com/it/u=113744206,1558321864&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=333"),
                                BannerBean("https://img2.baidu.com/it/u=3036691508,3668357645&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800"),
                            )
                        }

                        ui_Switch()
                        {
                            width = 60
                            isChecked = true
                        }

                        ui_Tab()
                        {
                            width = MATCH_PARENT
                            height = 50
                            viewPagerId = "viewPager"
                            index = 1
                            bg = KtColor.white
                            lineBg = ui_Shape { color = KtColor.pink; corner = 5 }
                            isScroll = false
                            scrollOffset = 0.0
                            onItemSelect()
                            { view, isSelect, i, bean ->
                                view as KtTextView
                                view.setTextColor(if (isSelect) KtColor.red else KtColor.text)
                            }
                            itemConvert()
                            { view, i, bean ->
                                view as KtTextView
                                bean as String
                                view.text = bean
                            }
                            datas = listOf("简传递单", "谁是说的", "是啊")
//                            datas = listOf("简传递单", "谁是说的", "是啊", "算是多", "多少岁", "滴撒的", "稍等", "f是的", "地方的", "是的是的")

                            ui_Text()
                            {
                                text = "京哈"
                                textColor = Color.RED
                                gravity = Gravity.CENTER
                                paddingH = 10
                                weight = 1.0
                            }
                        }

                        ui_ViewPager()
                        {
                            id = "viewPager"
                            height = 200
                            fragmentManager = supportFragmentManager
                            fragItems = listOf(
                                KtFragItem(TestFragment::class).addArg("name", "顺哥哥00111"),
                                KtFragItem(TestFragment::class).addArg("name", "顺哥哥122"),
                                KtFragItem(TestFragment::class).addArg("name", "顺哥哥133"),
                                KtFragItem(TestFragment::class).addArg("name", "顺哥哥144"),
                                KtFragItem(TestFragment::class),
                            )
                        }

                        ui_SlideLayout()
                        {
                            ui_Text()
                            {
                                width = MATCH_PARENT
                                height = 80
                                gravity = Gravity.CENTER
                                text = "坚实的收到货"
                                bg = KtColor.yellow
                            }

                            ui_Text()
                            {
                                width = 80
                                height = MATCH_PARENT
                                gravity = Gravity.CENTER
                                text = "删除"
                                isAlphaClick = true
                                textColor = KtColor.white
                                bg = KtColor.red
                            }
                        }

                        ui_Button()
                        {
                            text = "按钮"
                            height = 42
                            width = MATCH_PARENT
                            marginH = 8
                            isAlphaClick = true
                            textColor = KtColor.white
                            bg = KtShape.mainButton
                        }

                        ui_FlexLayout()
                        {
                            width = MATCH_PARENT
                            ui_Text()
                            {
                                text = "大大脚"
                                margin = 5
                                padding = 5
                                bg = KtColor.white
                            }
                            ui_Text()
                            {
                                text = "都是的"
                                margin = 5
                                padding = 5
                                bg = KtColor.white
                                width = 0
                                flexGrow = 2.0
                            }
                            ui_Text()
                            {
                                text = "大都市"
                                margin = 5
                                padding = 5
                                bg = KtColor.white
                                width = 0
                                flexGrow = 1.0
                            }
                        }

                        ui_CheckText()
                        {
                            text = "就是得到"
                            textSize = 14
                            bg = KtColor.bgDeep
                            padding = 5
                        }

                        ui_Text("textDemo")
                        {
//                            text = "大手大脚"
                            labelTitleColor = 0xFF00FF
                            labelTailColor = 0x0000FF
                            labelTitle = "价格："
                            labelText = "1223.5"
                            labelTail = "元"
                            textSize = 24
                            isAutoVisible = true
                            textColor = Color.GREEN
                            hint = "决胜巅峰"
                        }

                        ui_RelativeLayout()
                        {
                            ui_Text()
                            {
                                id = "button"
                                bg = KtColor.purple
                                text = "袄多好abc\nsds\nsdds是"
                            }
                            ui_Text()
                            {
                                below = "button"
                                toRightOf = "button"
                                bg = KtColor.white
                                text = "大大脚"
                            }
                        }

                        ui_Edit()
                        {
                            paddingH = 15
                            paddingV = 10
                            isClear = true
                            bg = KtColor.white
                            text = "地方抠脚大汉拒接电话丰富的森林里的看法垮落法"
                            hint = "请输入名称"
                        }

                        ui_View()
                        {
                            width = 50
                            height = 50
                            bgRes = R.mipmap.kt_icon_loading
                        }

                        ui_Text()
                        {
                            text = "就到河坊街东方红"
                        }
                    }
                }

                ui_LetterBar()
                {
                    width = 34
                    height = screenHeightDp * 0.6
                    textSize = 10
                    alignParentRight = true
                    centerVertical = true
                    bold = true
                    textColor = KtColor.text
                    chars = "1212121d1fsdfd12dsfds"
                    onLetterChanged()
                    { s, position ->
                        Log.d("runtime", "$s - $position")
                    }
                }
            }

            ui_FrameLayout()
            {
                width = screenWidthDp / 2
                height = MATCH_PARENT
                layoutGravity = Gravity.LEFT
                bg = KtColor.blue

                ui_Text()
                {
                    text = "的看法京东方"
                    layoutGravity = Gravity.CENTER
                }
            }
        }

        // 命令控制
//        textDemo.text = "0.54423".red.parseAsHtml()

        textDemo.labelTail = "斤"

//        task.stop()
    }

    private val listItem = ui_FrameLayout()
    {
        ui_LinearLayout()
        {
            ui_Image()
            {
                src = "https://img2.baidu.com/it/u=794528275,4064511912&fm=253&fmt=auto&app=138&f=JPEG?w=789&h=500"
            }

            ui_Text("textTitle")
            {
                text = "法等多个"
                textColor = Color.MAGENTA
                hint = "是"
                isAlphaClick = true
                onClick = {
                    Toast.makeText(baseContext, "几号回家辅导费", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    override fun onResume()
    {
        super.onResume()

        Log.d("runtime", supportFragmentManager.fragments.size.toString())
    }
}

fun String.c(c: String): String
{
    return "<font color='$c'>$this</font>"
}

inline val String.red: String get() = c("#ff0000")